#include "viewplayerscreen.h"
#include "ui_viewplayerscreen.h"
#include "player.h"
#include "dbconnect.h"
#include <QDebug>

viewplayerscreen::viewplayerscreen(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::viewplayerscreen)
{
    ui->setupUi(this);
    loadPlayerList();
}

viewplayerscreen::~viewplayerscreen()
{
    delete ui;
}

void viewplayerscreen::loadPlayerList(){

    DbConnect dbconnection = DbConnect();

    QString query_string = QString("Select * from players");
    QSqlQuery query;
    query.prepare(query_string);
    query.exec();
    while(query.next()) {
        Player* temp_player = new Player();


        temp_player->setFirstName(query.value(0).toString());
        temp_player->setLastName(query.value(2).toString());
        temp_player->setJerseyNumber(query.value(3).toInt());
        temp_player->setId(query.value(4).toInt());
        player_list.append(temp_player);
    }
    query.finish();


    foreach(Player *each_player, player_list) {
        ui->comboBox->addItem(each_player->getLastName(), QVariant(each_player->getId()));
    }






   // query.exec("SELECT * FROM players");
   // record = query.record();
   // int firstName = record.indexOf("first");
   // int middleName = record.indexOf("middle");
   // int lastName = record.indexOf("last");
   // QString string;
   // int index = 1;
   // while (query.next()){
   //     string.clear();
   //     string.append(query.value(firstName).toString()).append(" ").append(query.value(middleName).toString()).append(" ").append(query.value(lastName).toString());
   //     qDebug() << string;
   //     ui->comboBox->insertItem(index++, string);
   // }
    return;
}

void viewplayerscreen::fillBoxes() {

    int current_player_id = ui->comboBox->itemData(ui->comboBox->currentIndex()).toInt();

    //ui->firstNameInput_2
   // ui->middleNameInput_2
   // ui->lastNameInput_2
   // ui->jerseyNumberInput_2

}
